﻿--[[
   @brief    ui logic for user_guide ui
   @author   hqzheng
   @version  1.0
   @date     2017-07-08
--]]
user_guide_control = 
{
    login_guide_param = {
        url = LoadStr("USER_GUIDE_URL"),
        report_type = "login_user_guide",
        need_focus_home = true,
        guide_show_callback = function()
            user_guide_control.set_user_guide_is_show();
            return;
        end,
        guide_close_callback = function()
            user_guide_control.fire_really_show();
            return;
        end,
    };
    init = function()
        base_utility.log("user_guide_control.init called");
        ierd_tgp.tpf_ui.Require("common\\OssConfig.lua");
        ierd_tgp.tpf_ui.Require("wegame_user_guide\\model.lua");
        ierd_tgp.tpf_ui.Require("wegame_user_guide\\view.lua");

        TOssConfig.QueryCfg(user_guide_model.cfg_name, "user_guide_control.on_get_json_cfg", false);
        comm_center.listen_broadcast("i_broadcast_close_main_wnd", user_guide_control.on_close_main_wnd);
    end;

    inner_log = function (msg, core_msg)
        base_utility.log(string.format("[user_guide_control] %s", msg), core_msg);
    end;

    on_get_json_cfg = function (sFile, zJson)
        if (nil == zJson) or (nil == sFile) or (user_guide_model.cfg_name ~= sFile) then
            user_guide_control.inner_log("get config failed");
            return;
        end;

        local zData = zJson["data"];
        if (nil == zData) then
            user_guide_control.inner_log("Get data fail.");
            return;
        end;
        user_guide_model.user_guide_config = zData[1];
        if nil ~= user_guide_model.user_guide_config then
            user_guide_control.inner_log(string.format("[handle_user_guide_config]version:%s, switch:%s",
                tostring(user_guide_model.user_guide_config.version_id),
                tostring(user_guide_model.user_guide_config.switch)));
        end
    end;

    close = function(id, json_str)
    end;

    show_user_guide = function ()
        if false == user_guide_control.show_user_guide_core() or 
            (true == user_guide_model.has_get_show_flag and 
                false == user_guide_model.need_show_user_guide)then
            user_guide_control.fire_really_show();
        end
    end;

    show_user_guide_core = function ()
        if nil ~= user_guide_model.user_guide_config and 
            nil ~= user_guide_model.user_guide_config.switch and 
            "0" == user_guide_model.user_guide_config.switch then
            return false;
        end

        if base_utility.CLIENT_TYPE.OVERSEA_VER == base_utility.get_client_version_type() then
            -- 海外版本不显示新手引导
            return false;
        end

        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        local is_netbar = game_lib:get_bind_channel_id();
        -- 网吧版本不显示新手引导
        if 1 == is_netbar then
            return false;
        end

        -- 判断是否是launcher启动的wegaeme，如果是，则不显示新手引导
        local is_launcher = base_utility.is_type_of_launcher();
        if is_launcher == true then
            base_utility.log("we game is launched by game, not show user guide");
            return false;
        end;

        local is_develop_ver = game_lib:Is_Develop_ver(); 
        -- 开发者版本不显示新手引导
        if true == is_develop_ver then
            return false;
        end

        local is_hidle_quick_login = false;
        local login_mgr =
            base_utility.get_component_adapter("ILogin_mgr",
                "ierd_tgp.login_mgr.GetLoginMgr();");
        if login_mgr ~= nil then
            is_hidle_quick_login = login_mgr:IsHideQuickLogin();
        end
        -- 快速登录不显示新手引导
        if is_hidle_quick_login then
            base_utility.log("[show_user_guide_core]is_hidle_quick_login");
            return false;
        end

        user_guide_model.has_show_mainframe = true;

        user_guide_control.inner_log(string.format("[show_user_guide]is_get_show_flag:%s, need_show_user_guide:%s",
            tostring(user_guide_model.has_get_show_flag), tostring(user_guide_model.need_show_user_guide)), true);
        -- 如果已经获取到标志且需要显示新手引导，则显示新手引导
        if true == user_guide_model.has_get_show_flag and true == user_guide_model.need_show_user_guide then
            user_guide_view.open_user_guide_page(user_guide_control.login_guide_param);
        end

        return true;
    end;

    get_user_guide_is_show = function ()
        -- 先从本地获取
        local config = user_guide_model.get_user_guide_config();
        -- test userguide
        -- config = nil;
        -- 本地没有记录则通过cgi拉取
        if nil == config or 0 == config.exits then
            if nil ~= user_guide_model.user_guide_config and nil ~= user_guide_model.user_guide_config.version_id then
                base_utility.get_accumulate_count(user_guide_model.user_guide_item,
                    tostring(user_guide_model.user_guide_config.version_id),
                    tostring(user_guide_model.self_uin), "user_guide_control.on_get_user_guide_is_show", true);
            end
        else
             local local_config = base_utility.json_to_table(config.value);
             if nil ~= local_config and nil ~= user_guide_model.user_guide_config then
                 -- 如果本地的新手引导版本和服务器的不一致，则显示新手引导，同时调整主窗口尺寸为默认尺寸
                 if tostring(local_config.version) ~= tostring(user_guide_model.user_guide_config.version_id) then
                     user_guide_model.need_show_user_guide = true;
                 end
                 user_guide_model.has_get_show_flag = true;
             end
        end
    end;

    on_get_user_guide_is_show = function (json_str)
        local result_tb = base_utility.json_to_table(json_str);
        if nil == result_tb then
            return;
        end

        user_guide_control.inner_log(string.format("[on_get_user_guide_is_show]json_str:%s", json_str), true);

        if 0 == result_tb.result then
            local list = result_tb.sub_item_list;
            if type(list) ~= "table" or nil == next(list) then  
                user_guide_model.need_show_user_guide = true;
            else
                -- 当前的版本不在已读的列表中
                if nil ~= user_guide_model.user_guide_config and 
                    nil ~= user_guide_model.user_guide_config.version_id then
                    local version = user_guide_model.user_guide_config.version_id;
                    if false == user_guide_control.ver_is_in_list(version, result_tb.sub_item_list) then
                        user_guide_model.need_show_user_guide = true;
                    end
                end
            end

            user_guide_model.has_get_show_flag = true;
            -- 如果主界面已经显示且需要显示新手引导，则显示新手引导
            user_guide_control.inner_log(string.format("[show_user_guide]has_show_mainframe:%s, need_show_user_guide:%s",
                tostring(user_guide_model.has_show_mainframe), tostring(user_guide_model.need_show_user_guide)), true);
            if true == user_guide_model.has_show_mainframe and true == user_guide_model.need_show_user_guide then
                user_guide_view.open_user_guide_page(user_guide_control.login_guide_param);
            end

            -- 查询云端已显示过则保存本地
            if false == user_guide_model.need_show_user_guide then
                user_guide_control.set_user_guide_is_show();
            end
        end

        if false == user_guide_model.need_show_user_guide then
            user_guide_control.fire_really_show();
        end
    end;

    set_user_guide_is_show = function ()
        if nil ~= user_guide_model.user_guide_config and nil ~= user_guide_model.user_guide_config.version_id then
            local version = user_guide_model.user_guide_config.version_id;
            base_utility.set_accumulate_count(user_guide_model.user_guide_item,
                tostring(version), 1,
                tostring(user_guide_model.self_uin), "user_guide_control.on_set_user_guide_is_show");
            -- 写入本地配置
            user_guide_model.save_user_guide_config(tostring(version));
        end
    end;

    on_set_user_guide_is_show = function (json_str)
        local result_tb = base_utility.json_to_table(json_str);
        if nil == result_tb then
            return;
        end

        user_guide_control.inner_log(string.format("[on_set_user_guide_is_show]json_str:%s", json_str), true);

        if 0 == result_tb.result then
            user_guide_control.inner_log("on_set_user_guide_is_show set flag success");
        end
    end;
    
    -- 获取是否为网吧版本
    get_netbar_flag = function()
        -- body
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        local is_netbar = game_lib:get_bind_channel_id();
        user_guide_model.is_netbar = is_netbar;
    end;

    on_login_success = function(event_id, json_param)
        local table_param = base_utility.json_to_table(json_param);
        user_guide_model.self_uin = table_param.uin;
        user_guide_control.get_user_guide_is_show();
    end; 

    ver_is_in_list = function (version, list)
        if nil == list then
            return false;
        end

        local is_in_list = false;
        for k, v in pairs(list) do
            -- test userguide
            -- v.is_reported = 0;
            if tostring(v.sub_item) == tostring(version) and 0 ~= v.is_reported then
                is_in_list = true;
                break;
            end
        end
        
        return is_in_list;
    end;

    on_js_callback = function(uilib, func_name, json_str)
        user_guide_control.inner_log("on_js_callback, func_name:"..func_name..", json_str:"..json_str);
        local param_tbl = base_utility.json_to_table(json_str);
        if param_tbl == nil then
            user_guide_control.inner_log("on_js_callback parse json_str error", true);
            return;
        end

        if func_name == "jc_close_user_guide" then
            user_guide_control.inner_log("close userguide:" .. json_str);
            user_guide_view.close_user_guide();
        elseif func_name == "jc_current_page" then
            user_guide_control.inner_log("jc_current_page" .. json_str);
            user_guide_control.on_handle_cur_page(base_utility.json_to_table(json_str));
        end
        if user_guide_model.show_param ~= nil and
           user_guide_model.show_param.on_js_callback ~= nil then
            user_guide_model.show_param.on_js_callback(uilib, func_name, json_str);
        end
    end;

    fire_really_show = function()  
        if false == user_guide_model.has_notify_show then
            user_guide_control.inner_log("fire_really_show");                       
            local game_lib =
                base_utility.get_component_adapter("IGame_library",
                    "ierd_tgp.game_library.Get_game_library_adapter();");
            if game_lib ~= nil then
                local gamelib_event = { };
                local gamelib_event_str =  base_utility.table_to_json(gamelib_event);
                game_lib:OnGameLibraryEvent(ierd_tgp.game_library.MAIN_ON_REALLY_SHOW,
                    gamelib_event_str);
                user_guide_model.has_notify_show = true;
                user_guide_view.unload_web_component();
                comm_center.send_broadcast("i_broadcast_finsh_wegame_user_guide", {show = user_guide_model.need_show_user_guide});
            end
        end
        user_guide_model.has_finish_user_guide = true;
    end;

    on_handle_cur_page = function (param_tbl)
        base_utility.log("[user_guide_control][on_handle_cur_page]");
        if nil ~= param_tbl and nil ~= param_tbl.data and nil ~= param_tbl.data.page_index then
            user_guide_model.cur_page_index = param_tbl.data.page_index;
        end;
    end;

    on_close_main_wnd = function (data)
        user_guide_view.close_user_guide();
    end;
};

simple_user_guide_control = 
{

    log = function(msg, core_msg)
        base_utility.log(string.format("[simple_user_guide_control] %s", msg), core_msg);
    end;

    init = function()
        simple_user_guide_control.log("init");
        comm_center.reg_service("i_service_show_simple_user_guide"
                                        , "simple_user_guide_control.show_simple_user_guide_svr");
        comm_center.reg_service("i_service_query_simple_user_guide_show"
                                        , "simple_user_guide_control.query_show_user_guide_svr");
    end;

    check_simple_user_guide_should_show = function()
        if base_utility.CLIENT_TYPE.OVERSEA_VER == base_utility.get_client_version_type() then
            return false;
        end

        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
--        local is_netbar = game_lib:get_bind_channel_id();
--        if 1 == is_netbar then
--            return false;
--        end

        local is_launcher = base_utility.is_type_of_launcher();
        if is_launcher == true then
            return false;
        end;

        local is_develop_ver = game_lib:Is_Develop_ver();
        if true == is_develop_ver then
            return false;
        end

        return true;
    end;

    has_guide_showing = function()
        return user_guide_model.is_showing_user_guide;
    end;

    show_simple_user_guide = function(url, guide_type)
        if nil == url or "" == url then
            return false;
        end
        if not simple_user_guide_control.check_simple_user_guide_should_show() then
            return false;
        end

        if simple_user_guide_control.has_guide_showing() then
            return false;
        end

        local param = {
            url = url;
            report_type = guide_type;
        };
        user_guide_view.open_user_guide_page(param);

        return true;
    end;

    show_simple_user_guide_svr = function(params)
        if nil == params then return end;
        params.result = 0;
        if simple_user_guide_control.show_simple_user_guide(params.guide_url, params.guide_type) then
            params.result = 1;
        end
        return params;
    end;

    query_show_user_guide_svr = function()
        local params = { result = 0 };
        if not simple_user_guide_control.check_simple_user_guide_should_show() then
            return params;
        end
        if simple_user_guide_control.has_guide_showing() then
            return params;
        end
        params.result = 1;
        return params;
    end;

    show_custom_user_guide = function(params)
        if simple_user_guide_control.has_guide_showing() then
            return false;
        end
        user_guide_view.open_user_guide_page(params);
        return true;
    end;
};

user_guide_control.init();
simple_user_guide_control.init();
